package com.hymall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketRole;
import com.hymall.bean.MarketRoleExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.RoleOptionsVo;
import com.hymall.mapper.MarketRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 叶熠彬
 * @description: 角色管理
 * @date: 2022/7/16 23:04
 */

@Service
public class RoleServiceImpl implements RoleService {



    @Autowired
    MarketRoleMapper roleMapper;


    /**
     * 显示角色信息
     *
     * @author: 叶熠彬
     * @description:
     * @date: 2022/7/16 14:46
     * @param: []
     * @return: com.hymall.bean.common.CommonData
     */

    @Override
    public CommonData queryRoleOption() {
//        PageHelper.startPage(INT_ROLE_PAGE_NUM, INT_ROLE_PAGE_LIMIT);
        MarketRoleExample example = new MarketRoleExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketRole> marketRoles = roleMapper.selectByExample(example);
        List<RoleOptionsVo> vos = new ArrayList<>();
        for (MarketRole role : marketRoles) {
            RoleOptionsVo vo = new RoleOptionsVo();
            vo.setLabel(role.getName());
            vo.setValue(role.getId());
            vos.add(vo);
        }
        PageInfo<RoleOptionsVo> pageInfo = new PageInfo<>(vos);
        return CommonData.data(pageInfo);
    }

    /**
     * 角色管理：显示全部角色信息
     *
     * @author: 叶熠彬
     * @date: 2022/7/17 0:00
     * @param: [name, info]
     * @return: com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData queryRoleList(String name, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit(),info.getSort()+" "+info.getOrder());
        MarketRoleExample example = new MarketRoleExample();
        if (name != null) {
            example.createCriteria().andNameLike(name);
        }
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketRole> marketRoles = roleMapper.selectByExample(example);
        PageInfo<MarketRole> pageInfo = new PageInfo<>(marketRoles);

        return CommonData.data(pageInfo);
    }


    /**
     * 添加角色，其中角色名不可重复
     *
     * @author: 叶熠彬
     * @date: 2022/7/17 18:04
     * @param: [desc, name]
     * @return: com.hymall.bean.MarketRole
     */
    @Override
    public int addRole(MarketRole role) {
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(role.getName());
        criteria.andDeletedEqualTo(false);
        List<MarketRole> exist = roleMapper.selectByExample(example);
        if (exist.size() != 0) {
            //用户名已存在
            return 201;
        }
        Date date = new Date();
        role.setAddTime(date);
        role.setUpdateTime(date);
        role.setDeleted(false);
        role.setEnabled(true);
        try {
            roleMapper.insertSelective(role);
            return 200;
        } catch (Exception e) {
            //异常是因为name设置了unique，这里没法重新插入一条和deleted=1 name相同的角色
            // 改为update deleted=0
            MarketRoleExample marketRoleExample = new MarketRoleExample();
            marketRoleExample.createCriteria().andNameEqualTo(role.getName());
            //如果重名新角色的描述是空的，把之前的置空
            if (role.getDesc() == null) {
                role.setDesc("");
            }
            roleMapper.updateByExampleSelective(role, marketRoleExample);
            return 200;
        }
    }

    /**
     * 更新角色信息
     * 不允许重复的角色名称
     *
     * @author: 叶熠彬
     * @date: 2022/7/17 20:55
     * @param: [role]
     * @return: int
     */
    @Override
    public int updateRole(MarketRole role) {
        MarketRoleExample example = new MarketRoleExample();
        example.createCriteria().andNameEqualTo(role.getName());
        List<MarketRole> marketRoles = roleMapper.selectByExample(example);
        //如果角色名被占用
        if (marketRoles.size() != 0 && !marketRoles.get(0).getId().equals(role.getId())) {
            return 201;
        }

        //设置更新时间
        role.setUpdateTime(new Date());
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        marketRoleExample.createCriteria().andIdEqualTo(role.getId());
        try {
            roleMapper.updateByExampleSelective(role, marketRoleExample);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 500;
    }

    @Override
    public int deleteRole(MarketRole role) {
        role.setDeleted(true);
        try {
            roleMapper.updateByPrimaryKey(role);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 500;
    }
}
